package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
   /**
     * 新增分类
     * @param categoryDTO
     */
   @Autowired
    private CategoryMapper categoryMapper;
   @Autowired
    private DishMapper dishMapper;
   @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void save(CategoryDTO categoryDTO) {
        //1.拷贝属性
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        //2.设置状态1
        category.setStatus(StatusConstant.ENABLE);
        //3.设置创建人，创建时间
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        category.setCreateUser(BaseContext.getCurrentId());
//        category.setUpdateUser(BaseContext.getCurrentId());
        //4.插入数据
        categoryMapper.insert(category);

    }

    /**
     * 分类分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        //1.开始分页
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        //2.执行查询
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);
        //3.返回结果
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 删除分类
     * @param
     */
    @Override
    public void delete(Integer id) {
        //1.查询当前分类是否关联了套餐，如果已经关联，则无法删除
        int count = dishMapper.countByCategoryId(id);
        if(count > 0){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //2.套餐是否关联了菜品，如果已经关联，则无法删除
        count = setmealMapper.countByCategoryId(id);
        if(count > 0){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        //3.删除分类
        categoryMapper.delete(id);
    }

    /*
    * 修改状态
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //1.设置修改数据
        Category category = Category.builder()
                .status(status)
                .id(id)
                .build();
        categoryMapper.update(category);
    }



    /*
    修改
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        //1.拷贝
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        //2.设置修改时间
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(category);
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> list(Integer type) {
        return categoryMapper.list(type);
    }



}
